package cn.abcsys.cloud.devops.web.controller;


import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import cn.abcsys.cloud.devops.web.entity.Authority;
import cn.abcsys.cloud.devops.web.entity.GridBean;
import cn.abcsys.cloud.devops.web.entity.PagerModel;
import cn.abcsys.cloud.devops.web.entity.RoleAction;
import cn.abcsys.cloud.devops.web.service.AuthorityService;
import cn.abcsys.cloud.devops.web.service.RoleService;
import cn.abcsys.cloud.devops.web.util.Result;



/**
 * 用户信息控制器
 * @author xzg
 *
 */
@RestController
@RequestMapping("/permissions")
public class RoleActionController {
	private static final Logger LOGGER = Logger.getLogger(RoleActionController.class);
	@Resource
	private AuthorityService authorityService;
	@Resource
	private RoleService roleService;
	
	/**
	 * 查询角色信息
	 * @param listURE
	 * @return
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public GridBean selectUnderRole(HttpSession session, @RequestBody Map<String, Object> params){
		
		//List<Role> roleList = roleService.getRoleByName(role);
		if(null == params){
			LOGGER.error("请求参数为空");
			return null;
		}else{
			try{
				//JSONObject paramIn = JSONObject.parseObject(params);
				JSONObject paramIn = new JSONObject(params);
				PagerModel pagerModel = paramIn.toJavaObject(PagerModel.class);
				Authority authority = new Authority();
				authority.setAuthorityName(paramIn.getString("authorityName"));
				return authorityService.getAuthoritys(authority,session,pagerModel);
				//return roleService.getRoleByName(authority, pagerModel);
			}catch(Exception e){
				LOGGER.error("请求参数转换错误："+e.getMessage());
				return null;
			}
		}
	}
	
	/**
	 * 位角色批量增加权限
	 * @author xzg
	 * @param roleActionList
	 * @return
	 */
	@RequestMapping(value="/addRoleActions",method={RequestMethod.POST,RequestMethod.GET})
	public Result addRoleAction(List<RoleAction> roleActionList){
		
		int re = roleService.addRoleActions(roleActionList);
		
		if(0 == re){
			return new Result(false, "为角色赋予权限失败");
		}else{
			return new Result(true, "执行成功");
		}
	}
	/**
	 * 批量删除角色权限
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/delUnderRolesAction", method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Result delUnderRolesAction(List<Integer> list){
		int re = 0;
		try {
			 re=roleService.deleteRoleAction(list);
		} catch (SQLException e) {
		}
		if(0 == re)
			return new Result(false, "角色权限删除失败");
		else
			return new Result(true, "角色权限删除成功");
	}
	
	@RequestMapping(value="/updateAuthority",method={RequestMethod.POST})
	@ResponseBody
	public Result updateAuthority(@RequestBody Authority authority){
		int re = 0;
		try {
			 re=authorityService.updateAction(authority);
		} catch (SQLException e) {
		}
		if(0 == re)
			return new Result(false, "角色修改失败");
		else
			return new Result(true, "角色修改成功");
		
	}
}
